package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.PackingListDao;
import cn.itcast.dao.shipping.ShippingDao;
import cn.itcast.domain.cargo.PackingList;
import cn.itcast.domain.shipping.Shipping;
import cn.itcast.domain.shipping.ShippingExample;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service(timeout = 3000)
public class ShippingServiceImpl implements ShippingService {

    @Autowired
    private ShippingDao shippingDao;

    @Autowired
    private PackingListDao packingListDao;
    /**
     * 分页查询
     * @param shippingExample 分页查询的参数
     * @param pageNum         当前页
     * @param pageSize        页大小
     * @return
     */
    @Override
    public PageInfo<Shipping> findByPage(ShippingExample shippingExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList = shippingDao.selectByExample(shippingExample);
        PageInfo<Shipping> pageInfo = new PageInfo<>(shippingList);
        return pageInfo;
    }

    /**
     * 查询所有
     * @param shippingExample
     */
    @Override
    public List<Shipping> findAll(ShippingExample shippingExample) {
        return shippingDao.selectByExample(shippingExample);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Shipping findById(String id) {
        return shippingDao.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param shipping
     */
    @Override
    public void save(Shipping shipping) {
        PackingList packingList = packingListDao.selectByPrimaryKey(shipping.getShippingOrderId());
        packingList.setState("1");
        packingListDao.updateByPrimaryKeySelective(packingList);
        shippingDao.insert(shipping);
    }

    /**
     * 修改
     * @param shipping
     */
    @Override
    public void update(Shipping shipping) {
        shipping.setUpdateTime(new Date());
        shippingDao.updateByPrimaryKeySelective(shipping);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public Integer delete(String id) {
        Shipping shipping = shippingDao.selectByPrimaryKey(id);
        Integer state = shipping.getState();
        if (state == 0) {
            int row = shippingDao.deleteByPrimaryKey(id);
            return row;
        }else {
            return 0;
        }
    }
}
